﻿using MediatR;
using MyMES.Domain.W.Liststructure;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Read.API.Application.Commends.W.realize;

namespace MyMES.Read.API.Application.CommendHandlers.W.realize
{
    public class GetCallerCommandHanders : IRequestHandler<GetCallerCommands, APIPaging<List<Caller>>>
    {
        private readonly IBaseRepository<Caller> baseRepository;

        public GetCallerCommandHanders(IBaseRepository<Caller> baseRepository)
        {
            this.baseRepository = baseRepository;
        }

        /// <summary>
        /// 显示人员信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIPaging<List<Caller>>> Handle(GetCallerCommands request, CancellationToken cancellationToken)
        {
            APIPaging<List<Caller>> result = new APIPaging<List<Caller>>();
            result.Code = APIEnum.查询成功;
            result.Msg = "显示成功";
            var sql = "select * from Caller where IsDelete=false ORDER BY CallerId DESC";
            var list = await baseRepository.SelectAsync(sql);
            if (!string.IsNullOrEmpty(request.UserName))
            {
                list = list.Where(x => x.UserName.Contains(request.UserName)).ToList();
            }
            if (!string.IsNullOrEmpty(request.Phone))
            {
                list = list.Where(x => x.Phone.Contains(request.Phone)).ToList();
            }
            result.TotalCount = list.Count;
            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
            list = list.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            result.data = list;
            return result;
        }
    }
}
